# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/13
# @File   : R31_EC_PaidBusinessConcCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请企业已结清业务五级分类校验（关注类）
code:R31_EC_PaidBusinessConcCheck
tableName:
columnName:
groups:企业征信校验
dependencies:EC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def unpaidBusinessCheckPre(tableName):
    table = getTable(tableName)
    if table is None:
        return None
    elif table == [{}]:
        return 0
    else:
        result = []
        flag = []
        for i in table:
            try:
                if i['ReportNo'] not in null_type_list and i['Fiveclasscode'] == u'关注':
                    result.append(i['ReportNo'])
            except:
                flag.append(None)
                continue
        if len(flag) == len(table):
            return None
        else:
            return len(result)

def R31_EC_PaidBusinessConcCheck():

    # 处理 已结清信贷信息概要(7类信贷业务) 是汇总信息 仅有一条
    summaryList = {}
    ECRSummarySettled02 = getTable('ECRSummarySettled02')
    if ECRSummarySettled02 is None:
        summaryList = None
    elif ECRSummarySettled02 == [{}]:
        summaryList.update({u'贸易融资': 0, u'保理': 0,
                            u'银行承兑汇票': 0, u'信用证': 0, u'保函': 0, u'类贷款': 0})
    else:
        summaryDict = ECRSummarySettled02[0]
        flag = 0
        # 类贷款
        try:
            if summaryDict['ClassLoanCaresumNumber'] not in null_type_list:
                tmp = float(summaryDict['ClassLoanCaresumNumber'])
                summaryList.update({u'类贷款':tmp})
            else:
                summaryList.update({u'类贷款':0})
                flag += 1
        except:
            flag += 1
        # 贸易融资
        try:
            if summaryDict['FinanCaresumNumber'] not in null_type_list:
                tmp = float(summaryDict['FinanCaresumNumber'])
                summaryList.update({u'贸易融资':tmp})
            else:
                summaryList.update({u'贸易融资':0})
                flag += 1
        except:
            flag += 1
        # 保理
        try:
            if summaryDict['AssureeCaresumNumber'] not in null_type_list:
                tmp = float(summaryDict['AssureeCaresumNumber'])
                summaryList.update({u'保理':tmp})
            else:
                summaryList.update({u'保理':0})
                flag += 1
        except:
            flag += 1
        #银行承兑汇票
        try:
            if summaryDict['PostalCaresumNumber'] not in null_type_list:
                tmp = float(summaryDict['PostalCaresumNumber'])
                summaryList.update({u'银行承兑汇票': tmp})
            else:
                summaryList.update({u'银行承兑汇票': 0})
                flag += 1
        except:
            flag += 1
        #信用证
        try:
            if summaryDict['LetterCaresumNumber'] not in null_type_list:
                tmp = float(summaryDict['LetterCaresumNumber'])
                summaryList.update({u'信用证': tmp})
            else:
                summaryList.update({u'信用证': 0})
                flag += 1
        except:
            flag += 1
        #保函
        try:
            if summaryDict['GuaranteCaresumNumber'] not in null_type_list:
                tmp = float(summaryDict['GuaranteCaresumNumber'])
                summaryList.update({u'保函':tmp})
            else:
                summaryList.update({u'保函':0})
                flag += 1
        except:
            flag += 1
        # 如果6个字段全部有问题，则返回空 这里是6类
        if flag == 6:
            summaryList = None

    # 处理Factor1 类贷款
    if summaryList == None:
        Factor1 = None
    else:
        Factor1 = summaryList.get(u'类贷款')

    # 处理Factor2
    infoList2 = unpaidBusinessCheckPre('ECRPaidBadTradeFinancing')
    if infoList2 == None and summaryList == None:
        Factor2 = None
    else:
        if infoList2 == None:
            Factor2 = max(summaryList.get(u'贸易融资'), 0)
        elif summaryList == None:
            Factor2 = max(0, infoList2)
        else:
            Factor2 = max(summaryList.get(u'贸易融资'), infoList2)

    # 处理Factor3
    infoList3 = unpaidBusinessCheckPre('ECRPaidBadFactoring')

    if infoList3 == None and summaryList == None:
        Factor3 = None
    else:
        if infoList3 == None:
            Factor3 = max(summaryList.get(u'保理'), 0)
        elif summaryList == None:
            Factor3 = max(0, infoList3)
        else:
            Factor3 = max(summaryList.get(u'保理'), infoList3)

    # 处理Factor4
    infoList4 = unpaidBusinessCheckPre('ECRPaidBadBankAcceptance')

    if infoList4 == None and summaryList == None:
        Factor4 = None
    else:
        if infoList4 == None:
            Factor4 = max(summaryList.get(u'银行承兑汇票'), 0)
        elif summaryList == None:
            Factor4 = max(0, infoList4)
        else:
            Factor4 = max(summaryList.get(u'银行承兑汇票'), infoList4)

    # 处理Factor5
    infoList5 = unpaidBusinessCheckPre('ECRPaidBadLetterCredit')

    if infoList5 == None and summaryList == None:
        Factor5 = None
    else:
        if infoList5 == None:
            Factor5 = max(summaryList.get(u'信用证'), 0)
        elif summaryList == None:
            Factor5 = max(0, infoList5)
        else:
            Factor5 = max(summaryList.get(u'信用证'), infoList5)

    # 处理Factor6
    infoList6 = unpaidBusinessCheckPre('ECRPaidBadLetterGuarantee')

    if infoList6 == None and summaryList == None:
        Factor6 = None
    else:
        if infoList6 == None:
            Factor6 = max(summaryList.get(u'保函'), 0)
        elif summaryList == None:
            Factor6 = max(0, infoList6)
        else:
            Factor6 = max(summaryList.get(u'保函'), infoList6)


    # 处理7类业务明细和汇总表均为None 返回 缺失值
    if Factor1 is None and Factor2 is None and Factor3 is None \
            and Factor4 is None and Factor5 is None and Factor6 is None :
        return u'缺失值'
    else:
        if Factor1 is None:
            Factor1 = 0
        if Factor2 is None:
            Factor2 = 0
        if Factor3 is None:
            Factor3 = 0
        if Factor4 is None:
            Factor4 = 0
        if Factor5 is None:
            Factor5 = 0
        if Factor6 is None:
            Factor6 = 0
        return Factor1 + Factor2 + Factor3 + Factor4 + Factor5 + Factor6

result = R31_EC_PaidBusinessConcCheck()